package cn.ezios.huo.s3.controller;

import cn.ezios.huo.common.mvc.Ret;
import cn.ezios.huo.s3.bean.HuoStorageRespDto;
import cn.ezios.huo.s3.core.HuoStorageCoreWrapper;
import cn.ezios.huo.s3.dao.SysStorageFile;
import cn.ezios.huo.s3.dao.service.SysStorageFileService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RequestMapping("file/storage")
@Controller
@AllArgsConstructor
public class HuoStorageController {

    private final HuoStorageCoreWrapper wrapper;

    private final SysStorageFileService fileService;

    @PostMapping("upload-result")
    @ResponseBody
    public Ret uploadResult(@RequestBody SysStorageFile file) {
        boolean save = fileService.save(file);
        return save ? Ret.ok() : Ret.error();
    }

    @GetMapping("pre-upload")
    @ResponseBody
    public Ret preUpload(@RequestParam("fileName") String fileName) {
        HuoStorageRespDto dto = wrapper.getUploadUrl(fileName);
        return Ret.ok(dto);
    }

    @GetMapping("pre-preview")
    public String prePreview(@RequestParam("key") String key) {
        return wrapper.getPreviewUrl(key);
    }

    @GetMapping("preview")
    public String preview(@RequestParam("key") String key) {
        return "redirect:" + wrapper.getPreviewUrl(key);
    }

}
